<template>
    <Modal
            v-model="modelShow"
            title="批量导入图片"
            @on-ok="okAction"
            @on-cancel="cancelAction">
        <wl-upload-oss-files
                v-model="imgList"
                :maxNum="50"
                uploadType="hadoop"
                @result="resultAction"
                fileDir="gov"
                fileName="origin"
                :beforeUploadFilter="uploadFilter"
                :uploadUrl="getUploadUrl"
                :format="format">
        </wl-upload-oss-files>
    </Modal>
</template>

<script>
import { isNumber } from '@/libs/util'

export default {
  name: 'ImgImportService2',
  data () {
    return {
      modelShow: false,
      imgList: '',
      format: ['jpg', 'jpeg', 'png'],
      keyData: ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'],
      filterData: {}
    }
  },
  props: {
    options: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    modelAction () {
      this.modelShow = !this.modelShow
    },
    okAction () {
      this.$emit('result', this.filterData)
      this.modelAction()
    },
    resultAction (data) {
      let arrData = data.split(',')
      if (arrData.length > 0) {
        this.filterData = {}
        arrData.forEach((item, index) => {
          if (item !== undefined && item && item !== 'undefined') {
            let arr = this.getUrlImgName(item).split('.')
            let arr2 = arr[0].split('-')
            let keyMap = this.keyData[arr2[0]] + '_' + this.keyData[arr2[1]]
            if (this.filterData[keyMap] !== '' && this.filterData[keyMap] !== undefined) {
              this.filterData[keyMap] = this.filterData[keyMap] + ',' + item
            } else {
              this.filterData[keyMap] = item
            }
          }
        })
      }
    },
    cancelAction () {
      this.modelShow = false
    },
    getUrlImgName (url) {
      let pos = url.lastIndexOf('/')
      if (pos === -1) {
        pos = url.lastIndexOf('\\')
      }
      let filename = url.substr(pos + 1)
      if (filename.indexOf('?') !== -1) {
        let arr = filename.split('?')
        filename = arr[0]
      }
      return filename
    },
    uploadFilter (file, uploadList) {
      let isExit = false
      if (uploadList.length > 0) {
        uploadList.forEach(item => {
          if (item.url.indexOf(file.name) !== -1) {
            isExit = true
          }
        })
      }
      if (isExit) {
        this.$Notice.warning({
          title: file.name + '【文件已经存在】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      let arr = file.name.split('.')
      let arr2 = arr[0].split('-')
      if (arr2.length !== 4 || !isNumber(arr2[0]) || !isNumber(arr2[1]) || !isNumber(arr2[2]) || !isNumber(arr2[3])) {
        this.$Notice.warning({
          title: file.name,
          desc: '文件命名格式不正确必须全部是数字包含三个"-"',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[0]) < 1 || parseInt(arr2[0]) > 10 || !this.$common.xeUtils.isInteger(parseInt(arr2[0]))) {
        this.$Notice.warning({
          title: file.name + '【文件一级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[1]) < 1 || parseInt(arr2[1]) > 10 || !this.$common.xeUtils.isInteger(parseInt(arr2[1]))) {
        this.$Notice.warning({
          title: file.name + '【文件二级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[2]) < 1 || parseInt(arr2[2]) > 10 || !this.$common.xeUtils.isInteger(parseInt(arr2[2]))) {
        this.$Notice.warning({
          title: file.name + '【文件三级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[3]) < 1 || parseInt(arr2[3]) > 100 || !this.$common.xeUtils.isInteger(parseInt(arr2[3]))) {
        this.$Notice.warning({
          title: file.name + '【文件四级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      return { errcode: true }
    }
  },
  computed: {
    getUploadUrl () {
      return this.$config.upLoadFileUrl
    }
  }
}
</script>

<style scoped>

</style>
